<?php

namespace App\Admin\Controllers\Commodity;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Commodity;

/**
 * Class ApiCommodityController
 * @package App\Admin\Controllers\Commodity
 * @User wuqin
 * @Date 2018/3/21 0021
 * @Time 15:38
 */
class ApiCommodityController extends Controller
{

    /**
     * 方法 : get_children_commodity
     * @User wuqin
     * @DateTime 2018-03-21 15:38
     * @param Request $request
     */
    public function get_children_commodity(Request $request)
    {
        $data = $request->input();
        if (empty($data) || empty($data["classify"]))
            return return_array(1, '非法参数');
        //获取该分类商品
        $commodityModel = new Commodity();
        $commodity = $commodityModel->get_commodity_by_classify_id((int)$data['classify']);
        if(empty($commodity))
            return return_array(1,'空');
        else
            return return_array(0,'OK',$commodity);
    }

    /**
     * 方法 : add_commodity
     * @User wuqin
     * @DateTime 2018-03-21 15:38
     * @param Request $request
     */
    public function add_commodity(Request $request)
    {
        $data = $request->input();
        if (empty($data) || empty($data["classify_ids"]))
            return return_array(1, '非法参数');
        $data['commodity_classify_ids'] = '';
        foreach ($data['classify_ids'] as $key => $value) {
            $data['commodity_classify_ids'] .= $key.',';
        }
        $data = array(
            'commodity_classify_ids' => $data['commodity_classify_ids'],
            'commodity_name'=>$data['name'],
            'commodity_title' => $data['title'],
            'index_img_url'=>$data['url'],
            'commodity_desc'=>$data['content']
        );
        $commodity = new Commodity();
        if(!empty($commodity->add_commodity($data)))
            return return_array(0,'yes');
        else
            return return_array(1,'未知错误');
    }
}
